package com;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;

public class J计算器 implements ActionListener{
    JFrame frame = new JFrame("计算器");
    String[] str = {"%","CE","C","back","1/x","x²","²√x","÷","7","8","9","×","4","5","6","-","1","2","3","+","+/-","0",".","="};
    boolean first = true, flag = true;        //first判断是否为第一次输入，flag判断操作是否正确
    double Num;
    String operator = "=", temp = "",temp1;
    JButton button[] = new JButton[str.length];
    JTextArea result1 = new JTextArea();      //公式显示框对象
    JTextArea result2 = new JTextArea("0");   //输入显示框对象
    JTextArea history= new JTextArea();       //历史记录文本框对象
    JScrollPane s1=new JScrollPane(result2);  //给输入显示屏文本域新建一个垂直滚动滑条
    JScrollPane s2=new JScrollPane(history);  //给历史记录文本域新建一个垂直滚动滑条
    JScrollPane s3=new JScrollPane(result1);  //给公式显示框新建一个垂直滚动条
    JLabel label = new JLabel("历史记录");

    //构造方法
    public J计算器(){
        result1.setBounds(10,10,400,50);
        result1.setEditable(false);           //公式显示框设置为不可编辑
        result1.setBorder(null);
        result1.setFont(new Font("宋体",Font.BOLD,20));
        result2.setBounds(10,50,400,150);
        //result2.setAlignmentX(2);
        result2.setEditable(false);           //输入显示框设置为不可编辑
        result2.setBorder(null);
        result2.setFont(new Font("宋体",Font.BOLD,30));
        history.setBounds(410,40,400,620);
        //history.setAlignmentX(2);
        history.setEditable(false);           //历史记录文本框设置为不可编辑
        history.setBorder(null);
        history.setFont(new Font("宋体",Font.BOLD,20));
        label.setBounds(420,10,100,20);
        s1.setViewportView(result2);         //使滚动条显示出来
        s2.setViewportView(history);
        s3.setViewportView(result1);
        s1.setBorder(null);
        s2.setBorder(null);
        s3.setBorder(null);
    }

    //建立布局
    public void init() {
        Panel p1 = new Panel();
        p1.setBounds(10,10,400,150);
        p1.setLayout(new BorderLayout());
        Panel p2 = new Panel();
        p2.setBounds(410,40,400,620);
        p2.setLayout(new GridLayout());
        Panel p3 = new Panel();
        p3.setBounds(10,160,400,500);
        p3.setLayout(new GridLayout(6,4,2,2));
        for(int i=0;i<str.length;i++) {
            button[i] = new JButton(str[i]);
            button[i].setBorderPainted(false);
            button[i].setFont(new Font("宋体",Font.BOLD,20));
            p3.add(button[i]);
        }
        for(int i=0;i<8;i++) {
            button[i].setBackground(Color.lightGray);
        }
        button[11].setBackground(Color.lightGray);
        button[15].setBackground(Color.lightGray);
        button[19].setBackground(Color.lightGray);
        button[23].setBackground(Color.green);
        for(int i=0;i<str.length;i++) {
            button[i].addActionListener(this);       //为按钮注册监视器
        }
        p1.add(s3,BorderLayout.NORTH);
        p1.add(s1);
        p2.add(s2);
        frame.add(p1);
        frame.add(p2);
        frame.add(p3);
        frame.add(label);
        frame.setLayout(null);
        frame.setBounds(500,70,820,700);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    //监视器处理事件
    public void actionPerformed(ActionEvent e) {

        String doing = e.getActionCommand();  //获取事件
        if(doing.equals(str[3])) {
            doBack();         //按下退格键
        }
        else if(doing.equals(str[2])) {
            doC();            //按下CE键
        }
        else if(doing.equals(str[1])) {
            doC();            //按下删除键
        }
        else if ("0123456789.".indexOf(doing)>= 0) {
            doNumber(doing);        //按下数字和小数点

        }
        else if(doing.equals("%")) {
            doper(doing);           //按下百分号
        }
        else if(doing.equals("1/x")) {
            docount(doing);           //按下倒数
        }
        else if(doing.equals("x²")) {
            dosqr(doing);           //按下平方
        }
        else if(doing.equals("²√x")) {
            doroot(doing);           //按下开方
        }
        else if(doing.equals("+/-")) {
            dooppo(doing);           //按下取正负数运算键
        }
        else if(doing.equals("÷")||doing.equals("×")||doing.equals("-")||doing.equals("+")||doing.equals("=")){
            docom(doing);             //按下加减乘除和等号键
        }
    }

    //执行退格
    public void doBack() {
        String text = result2.getText();
        int i = text.length();
        if(i > 0) {
            text = text.substring(0,i-1);
            if(i==0){
                result2.setText("0");
            }else {
                result2.setText(text);
            }
        }
    }

    //执行删除
    public void doC(){
        result2.setText("0");
    }

    //执行CE
    public void doCE(){
        result2.setText("0");
    }

    //执行数字和小数点按键操作
    public void doNumber(String str) {
        if(first) {
            if(str.equals(".")) {
                result2.setText("0.");
                temp="0.";
                history.setText(temp);
            }else {
                result2.setText(str);
                temp = temp+str;
                history.setText(history.getText()+str);
            }
        }
        else if((str.equals("."))&&(result2.getText().indexOf(".")==-1)) {
            result2.setText(result2.getText()+".");
            temp = history.getText()+".";
            history.setText(temp);
        }
        else if(!str.equals(".")) {
            result2.setText(result2.getText()+str);
            temp = history.getText()+str;
            history.setText(temp);
        }
        first = false;
    }

    //执行倒数操作
    public void docount(String str) {
        operator = str;
        temp = result2.getText();
        Num = Double.parseDouble(temp);
        if(operator.equals("1/x")) {
            if(Num==0) {
                result2.setText("除数不能为零");
                history.setText(history.getText()+": 除数不能为零"+"\n");
                flag = false;
            }if (flag) {
                // 操作合法的情况下，结果为小数保留小数点后5位，整数正常输出
                double t1, t2;
                Double dou = 1/Num;
                t1 = dou;
                t2 = dou - t1;
                if (t2 == 0) {
                    result1.setText("1/"+temp);
                    result2.setText(String.valueOf(t1));
                    history.setText(history.getText()+"的倒数是："+String.valueOf(t1)+"\n");
                }else {
                    result1.setText("1/"+temp);
                    result2.setText(String.valueOf(new DecimalFormat("0.00000").format(t1)));
                    history.setText(history.getText()+"的倒数是："+String.valueOf(new DecimalFormat("0.00000").format(t1))+"\n");
                }
            }
        }
        first = true;
    }

    //执行平方操作
    public void dosqr(String str) {
        operator = str;
        temp = result2.getText();
        if (operator.equals("x²")) {
            Num = Double.valueOf(temp)*Double.valueOf(temp);
            flag = true;
        }
        if (flag) {
            // 操作合法的情况下，结果为小数保留小数点后5位，整数正常输出
            double t1, t2;
            t1 = Num;
            t2 = Num - t1;
            if (t2 == 0) {
                result1.setText("sqr("+temp+")");
                result2.setText(String.valueOf(t1));
                history.setText(history.getText()+"的平方是：sqr("+temp+") = "+String.valueOf(t1)+"\n");
            }else {
                result1.setText("sqr("+temp+")");
                result2.setText(String.valueOf(new DecimalFormat("0.00000").format(t1)));
                history.setText(history.getText()+"的平方是：sqr("+temp+") = "+String.valueOf(new DecimalFormat("0.00000").format(t1))+"\n");
            }
        }

        first = true;
    }

    //执行根号操作
    public void doroot(String str) {
        operator = str;
        temp = result2.getText();
        Num = Double.parseDouble(temp);
        if(operator.equals("²√x")) {
            if(Num<0) {
                result2.setText("根号下不能为负");
                flag = false;
            }else {
                Num = Math.sqrt(Double.valueOf(result2.getText()));
                flag = true;
            }
        }
        if (flag) {
            // 操作合法的情况下，结果为小数保留小数点后5位，整数正常输出
            double t1, t2;
            t1 = Num;
            t2 = Num - t1;
            if (t2 == 0) {
                result1.setText("²√"+temp);
                result2.setText(String.valueOf(t1));
                history.setText(history.getText()+"的开方是：²√"+temp+" = "+String.valueOf(t1)+"\n");
            }else {
                result1.setText("²√"+temp);
                result2.setText(String.valueOf(new DecimalFormat("0.00000").format(t1)));
                history.setText(history.getText()+"的开方是：²√"+temp+" = "+String.valueOf(new DecimalFormat("0.00000").format(t1))+"\n");
            }
        }
        first = true;
    }

    //执行百分数操作
    public void doper(String str) {
        operator = str;
        temp = result2.getText();
        if(operator.equals("%")){
            Num = Double.valueOf(result2.getText())/100;
            flag = true;
        }
        if (flag) {
            // 操作合法的情况下，结果为小数保留小数点后5位，整数正常输出
            double t1, t2;
            t1 = Num;
            t2 = Num - t1;
            if (t2 == 0) {
                result1.setText(temp+"/100");
                result2.setText(String.valueOf(t1));
                history.setText(history.getText()+"的百分数是："+String.valueOf(t1)+"\n");
            }else {
                result1.setText(temp+"/100");
                result2.setText(String.valueOf(new DecimalFormat("0.00000").format(t1)));
                history.setText(history.getText()+"的百分数是："+String.valueOf(new DecimalFormat("0.00000").format(t1))+"\n");
            }
        }
        first = true;
    }

    //执行取正负数操作
    public void dooppo(String str) {
        operator = str;
        temp = result2.getText();
        if(operator.equals("+/-")){
            Num = Double.valueOf(result2.getText())*(-1);
            flag = true;
        }
        if (flag) {
            // 操作合法的情况下，结果为小数保留小数点后5位，整数正常输出
            double t1, t2;
            t1 = Num;
            t2 = Num - t1;
            if (t2 == 0) {
                result1.setText("-"+temp+"=");
                result2.setText(String.valueOf(t1));
                history.setText(history.getText()+"的相反数是："+String.valueOf(t1)+"\n");
            }else {
                result1.setText("-"+temp+"=");
                result2.setText(String.valueOf(new DecimalFormat("0.00000").format(t1)));
                history.setText(history.getText()+"的相反数是："+String.valueOf(new DecimalFormat("0.00000").format(t1))+"\n");
            }
        }
        first = true;
    }

    //执行加减乘除操作
    public void docom(String str) {
        String temp2;
        if ("+-×÷".indexOf(str) != -1) {
            temp1 = result2.getText();
            temp = history.getText() + str;
            operator = str;
            history.setText(temp);
            first = true;
        } else if ("=".equals(str)) {
            temp2 = result2.getText();
            double d1 = Double.parseDouble(temp1);
            double d2 = Double.parseDouble(temp2);
            if (operator.equals("+")) {
                d1 = d1 + d2;
                result2.setText(d1 + "");
                temp = temp + "=" ;
                first = true;
                flag = true;
            } else if (operator.equals("-")) {
                d1 = d1 - d2;
                result2.setText(d1 + "");
                temp = temp + "=";
                first = true;
                flag = true;
            } else if (operator.equals("×")) {
                d1 = d1 * d2;
                result2.setText(d1 + "");
                temp = temp + "=";
                first = true;
                flag = true;
            } else if (operator.equals("÷")) {
                if(d2==0) {
                    result2.setText("除数不能为零");
                    history.setText(temp+": 除数不能为零"+"\n");
                    first = true;
                    flag = false;
                }else {
                    d1 = d1 / d2;
                    result2.setText(d1 + "");
                    temp = temp + "=";
                    first = true;
                    flag = true;
                }
            }
            if (flag) {
                // 操作合法的情况下，结果为小数保留小数点后5位，整数正常输出
                long t1;
                double t2;
                t1 = (long)d1;
                t2 = d1 - t1;
                if (t2 == 0) {
                    result1.setText(temp1+operator+temp2+"=");
                    result2.setText(String.valueOf(t1));
                    history.setText(temp+String.valueOf(t1)+"\n");
                } else {
                    result1.setText(temp1+operator+temp2+"=");
                    result2.setText(String.valueOf(new DecimalFormat("0.00000").format(d1)));
                    history.setText(temp+String.valueOf(new DecimalFormat("0.00000").format(d1))+"\n");
                }
            }
        }
    }

    public static void main(String[] args) {
        J计算器 huan = new J计算器();
        huan.init();
    }
}

